import fs from "fs";
import path from "path";
const sqlite = require("sqlite3").verbose();

const db_file = path.join(__dirname, "../../../train_tools.db");

if (!fs.existsSync(db_file)) {
    fs.writeFileSync(db_file, "", { flag: "a" });
}

const db = new sqlite.Database(db_file, err => {
    if (err != null) {
        console.log("db", err);
    } else {
        const sql_train = "create table if not exists train (id integer primary key autoincrement,subject int(2),kind int(2),area nvarchar,addr nvarchar,chain_shop nvarchar,start_time nvarchar,end_time nvarchar,type int(2),require nvarchar,ppt_path nvarchar,len int,summary nvarchar,feedback nvarchar,effect nvarchar,parent_id int)";
        const sql_trip = "CREATE TABLE if not exists trip ( id integer PRIMARY KEY AUTOINCREMENT,title nvarchar,start_time navarchar,end_time nvarchar,area nvarchar,remark nvarchar)";
        db.run(sql_train, (res, err) => {
            if (err != null) {
                console.log('db', err);
            }
        });
        db.run(sql_trip);
    }
});

export { db };